package com.ophiux.rabbitmq.event;

import cn.hutool.core.util.ObjectUtil;

import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import com.ophiux.common.core.utils.SpringContextHolder;

/**
 * @desc: 监听远程事件, 并分发消息到业务模块消息处理器
 * @author: hhl
 * @date：2021/11/4 9:29
 */
@Component
public class BaseApplicationEvent implements ApplicationListener<OphiuxRemoteApplicationEvent> {

    @Override
    public void onApplicationEvent(OphiuxRemoteApplicationEvent ophiuxRemoteApplicationEvent) {
        EventObj eventObj = ophiuxRemoteApplicationEvent.getEventObj();
        if (ObjectUtil.isNotEmpty(eventObj)) {
            //获取业务模块消息处理器
            OphiuxBusEventHandler busEventHandler = SpringContextHolder.getHandler(eventObj.getHandlerName(), OphiuxBusEventHandler.class);
            if (ObjectUtil.isNotEmpty(busEventHandler)) {
                //通知业务模块
                busEventHandler.onMessage(eventObj);
            }
        }
    }

}
